import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-15
 * Time: 10:41
 */

public class TestDemo {

    public static void main(String[] args) throws FileNotFoundException {
        System.out.println(readFile());
    }
    public static String readFile() throws FileNotFoundException {
        // 尝试打开文件, 并读其中的一行.
        File file = new File("d:/test.txt");
        // 使用文件对象构造 Scanner 对象.
        Scanner sc = new Scanner(file);
        return sc.nextLine();
    }

    public static void main8(String[] args) {
        System.out.println(divide(10, 0));
    }
    public static int divide(int x, int y) throws ArithmeticException{
        if (y == 0) {
            throw new ArithmeticException("抛出除 0 异常");
        }
        return x / y;
    }

    public static int func(){
        int a = 10;
        try {
            return a;
        }catch (ArithmeticException e){
            e.printStackTrace();
        }finally {
            return 20;
        }
    }
    public static void main7(String[] args) {
        System.out.println(func());
    }
    public static void func1() {
        int[] arr = {1, 2, 3};
            System.out.println(arr[100]);
    }
    public static void main6(String[] args) {
        func1();
        System.out.println("after try catch");
    }

    public static void main5(String[] args) {
        try(Scanner scanner = new Scanner(System.in)) {
            int n  = scanner.nextInt();
            System.out.println(10/n);
        }catch (InputMismatchException e){
            e.printStackTrace();
            System.out.println("输入有误！");
        }catch (ArithmeticException e){
            e.printStackTrace();;
            System.out.println("算术异常，可能0作为了除数");
        }
    }

    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int n  = scanner.nextInt();
            System.out.println(10/n);
        }catch (InputMismatchException e){
            e.printStackTrace();
            System.out.println("输入有误！");
        }catch (ArithmeticException e){
            e.printStackTrace();;
            System.out.println("算术异常，可能0作为了除数");
        }finally {
            //用作资源关闭
            scanner.close();
            System.out.println("finally执行了");
        }
    }

    public static void main3(String[] args) {
            int[] arr = {1, 2, 3};
            try {
                System.out.println("before");
                arr = null;
                System.out.println(arr[100]);
                System.out.println("after");
            } catch (Exception e) {
                // 打印出现异常的调用栈
                e.printStackTrace();
                System.out.println("捕捉到了一个异常");
            }
            System.out.println("after try catch");
    }


    public static void main2(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (ArrayIndexOutOfBoundsException e) {
            // 打印出现异常的调用栈
            e.printStackTrace();
            System.out.println("捕捉到了一个数组越界异常");
        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("捕捉到了一个空指针异常");
        }
        System.out.println("after try catch");
    }

    public static void main1(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println("before");
        System.out.println(arr[100]);
        System.out.println("after");
    }
}
